<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use app\admin\model\Pic;
use think\Model;

class Carousel extends Model{
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order = 'create_time desc',$limit=5){
        $info = $this
            ->field('update_time',true)
            ->with('article')
            ->where($where)
            ->limit($limit)
            ->order($order)
            ->select();
        return $info;
    }

    /**
     * @param $id
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneById($id){
        $info = $this
            ->field('update_time',true)
            ->with('article')
            ->where('id','eq',$id)
            ->find();
        return $info;
    }

    public function article(){
        return $this->hasOne('Article','id','rid');
    }

    /**
     * @param $value
     * @return array|bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getImageAttr($value){
        if(empty($value)){
            return '';
        }
        $pic = new Pic();
        return $pic->getUrlsById($value);
    }
}